package com.starsoft.trade.plugin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.plugin.AbstractAcountPlugin;
import com.starsoft.smdc.util.SmdcConst;
import com.starsoft.trade.bean.TradeAcount;
import com.starsoft.trade.service.AcountStatusService;
import com.starsoft.trade.service.TradeAcountService;
import com.starsoft.trade.service.TradeBillService;
import com.starsoft.trade.service.TradeFeeService;

@Component
public class TradeAcountPluginImpl extends AbstractAcountPlugin{

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	AcountStatusService acountStatusService;
	@Autowired
	TradeFeeService tradeFeeService;
	@Autowired
	TradeBillService tradebillServie;
	@Autowired
	TradeAcountService tradeAcountService;

	@Override
	public String getName() {
		return "Trade";
	}
	
	public int getOrder(){
		return 1;
	}
	
	public void delete(Integer acountId){
		
	}

	public BaseAcount create(BaseAcount acount){
		logger.info("call trade acount Plugin");
		return acount;
	}
	

	@Override
	public void update(BaseAcount acount) {
		Integer acountId = acount.getAcountId();
		TradeAcount tradeAcount = tradeAcountService.get(acountId);
		acountStatusService.deleteAcountAll(acountId);
		tradebillServie.deleteBillsBefore(acountId, tradeAcount.getInitDate());
	}

	@Override
	public void delete(BaseAcount acount) {
		Integer acountId = acount.getAcountId();
		acountStatusService.deleteAcountAll(acountId);
		tradebillServie.deleteBillsAfter(acountId, SmdcConst.SMDC_BEGIN_DATE);
		tradeFeeService.deleteAll(acountId);
		tradeAcountService.delete(acountId);
	}

	public BaseAcount copy(BaseAcount from, BaseAcount to) {
		TradeAcount tradeFrom = tradeAcountService.get(from.getAcountId());
		if(tradeFrom==null){
			return to;
		}
		TradeAcount tradeTo = tradeAcountService.get(to.getAcountId());
		if( tradeTo == null ) {
			tradeTo = new TradeAcount(to.getAcountId());
		}
		boolean updateStatus = false;
		if (tradeFrom.getCmpIndex() == null && tradeTo.getCmpIndex() != null) {
			updateStatus = true;
		} else if (tradeFrom.getCmpIndex() != null && !tradeFrom.getCmpIndex().equals(tradeTo.getCmpIndex())) {
			updateStatus = true;
		}
		if(!tradeFrom.getInitDate().equals(tradeTo.getInitDate())){
			updateStatus = true;
		}
		if(!tradeFrom.getInitCash().equals(tradeTo.getInitCash())){
			updateStatus = true;
		}
		if(!tradeFrom.getInitPos().equals(tradeTo.getInitPos())){
			updateStatus = true;
		}
		
		tradeTo.setInitDate(tradeFrom.getInitDate());
		tradeTo.setInitCash(tradeFrom.getInitCash());
		tradeTo.setInitPos(tradeFrom.getInitPos());
		tradeTo.setEndDate(tradeFrom.getEndDate());
		tradeTo.setCmpIndex(tradeFrom.getCmpIndex());
		tradeAcountService.saveOrUpdate(tradeTo);
		tradeFeeService.deleteAndCopy(from.getAcountId(), to.getAcountId());
		if(updateStatus){
			acountStatusService.deleteAcountAll(tradeTo.getAcountId());
			tradebillServie.deleteBillsBefore(tradeTo.getAcountId(), tradeTo.getInitDate());
		}
		return to;
	}

	@Override
	public BaseAcount copyAsSun(BaseAcount parent, BaseAcount sun) {
		return copy(parent, sun);
	}

}
